//
// Copyright 2023 Google LLC
// SPDX-License-Identifier: Apache-2.0
//

// go/keep-sorted start
@use 'sass:list';
@use 'sass:map';
// go/keep-sorted end
// go/keep-sorted start
@use '../../elevation/elevation';
@use '../../focus/focus-ring';
@use '../../ripple/ripple';
@use '../../tokens';
// go/keep-sorted end

@mixin styles() {
  :host {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    outline: none;
    padding: 0 16px;
    position: relative;
    -webkit-tap-highlight-color: transparent;
    vertical-align: middle;
    user-select: none;
    font-family: var(--_label-text-font);
    font-size: var(--_label-text-size);
    line-height: var(--_label-text-line-height);
    font-weight: var(--_label-text-weight);
    color: var(--_label-text-color);
    z-index: 0; // Ensure this is a stacking context so the indicator displays

    @include ripple.theme(
      (
        hover-color: var(--_hover-state-layer-color),
        hover-opacity: var(--_hover-state-layer-opacity),
        pressed-color: var(--_pressed-state-layer-color),
        pressed-opacity: var(--_pressed-state-layer-opacity),
      )
    );

    // b/316459034 - elevation should be removed
    @include elevation.theme(
      (
        level: var(--_container-elevation),
      )
    );
  }

  md-focus-ring {
    @include focus-ring.theme(
      (
        'shape': 8px,
      )
    );
  }

  :host([active]) md-focus-ring {
    margin-bottom: calc(var(--_active-indicator-height) + 1px);
  }

  .button::before {
    background: var(--_container-color);
    content: '';
    inset: 0;
    position: absolute;
    z-index: -1;
  }

  .button::before,
  md-ripple,
  md-elevation {
    border-start-start-radius: var(--_container-shape-start-start);
    border-start-end-radius: var(--_container-shape-start-end);
    border-end-end-radius: var(--_container-shape-end-end);
    border-end-start-radius: var(--_container-shape-end-start);
  }

  .content {
    position: relative;
    box-sizing: border-box;
    display: inline-flex;
    flex-direction: row;
    align-items: center;
    justify-content: center;
    height: var(--_container-height);
    gap: 8px;
  }

  .indicator {
    position: absolute;
    box-sizing: border-box;
    z-index: -1;
    transform-origin: bottom left;
    background: var(--_active-indicator-color);
    border-radius: var(--_active-indicator-shape);
    height: var(--_active-indicator-height);
    inset: auto 0 0 0;
    // hidden unless the tab is selected
    opacity: 0;
  }

  // unselected states
  ::slotted([slot='icon']) {
    display: inline-flex;
    position: relative;
    writing-mode: horizontal-tb;
    fill: currentColor;
    color: var(--_icon-color);
    font-size: var(--_icon-size);
    width: var(--_icon-size);
    height: var(--_icon-size);
  }

  :host(:hover) {
    color: var(--_hover-label-text-color);
    cursor: pointer;
  }

  :host(:hover) ::slotted([slot='icon']) {
    color: var(--_hover-icon-color);
  }

  :host(:focus) {
    color: var(--_focus-label-text-color);
  }

  :host(:focus) ::slotted([slot='icon']) {
    color: var(--_focus-icon-color);
  }

  :host(:active) {
    color: var(--_pressed-label-text-color);
  }

  :host(:active) ::slotted([slot='icon']) {
    color: var(--_pressed-icon-color);
  }

  // selected styling
  :host([active]) .indicator {
    opacity: 1;
  }
  :host([active]) {
    color: var(--_active-label-text-color);

    @include ripple.theme(
      (
        hover-color: var(--_active-hover-state-layer-color),
        hover-opacity: var(--_active-hover-state-layer-opacity),
        pressed-color: var(--_active-pressed-state-layer-color),
        pressed-opacity: var(--_active-pressed-state-layer-opacity),
      )
    );
  }

  :host([active]) ::slotted([slot='icon']) {
    color: var(--_active-icon-color);
  }

  // selected states
  :host([active]:hover) {
    color: var(--_active-hover-label-text-color);
  }

  :host([active]:hover) ::slotted([slot='icon']) {
    color: var(--_active-hover-icon-color);
  }

  :host([active]:focus) {
    color: var(--_active-focus-label-text-color);
  }

  :host([active]:focus) ::slotted([slot='icon']) {
    color: var(--_active-focus-icon-color);
  }

  :host([active]:active) {
    color: var(--_active-pressed-label-text-color);
  }

  :host([active]:active) ::slotted([slot='icon']) {
    color: var(--_active-pressed-icon-color);
  }

  :host,
  ::slotted(*) {
    white-space: nowrap;
  }

  @media (forced-colors: active) {
    .indicator {
      background: CanvasText;
    }
  }
}
